package com.example.javaserve.controller;

/**
 * @projectName: my_project
 * @package: com.example.javaserve.controller
 * @className: AuthController
 * @author: xxXPCSC
 * @description: TODO
 * @date: 2023/11/15 16:07
 * @version: 1.0
 **/




import com.example.javaserve.dto.AuthUserInfoDto;
import com.example.javaserve.dto.AuthUserRolesDto;
import com.example.javaserve.dto.AuthUsername;


import com.example.javaserve.entity.MyResult;
import com.example.javaserve.result.Result;
import com.example.javaserve.result.impl.ErrorResult;
import com.example.javaserve.result.impl.SuccessResult;
import com.example.javaserve.service.AuthService;
import com.example.javaserve.dto.AuthUserDto;
import com.example.javaserve.service.UserBaseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * api登录授权
 *
 * @author zhuhuix
 * @date 2020-03-30
 */
@Slf4j
@RestController
@Api(tags = "系统授权接口")
public class AuthController {

    private final AuthService authService;

    @Autowired
    private UserBaseInfoService userBaseInfoService;

    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    @ApiOperation("发送邮箱验证码")
    @PostMapping(value = "/getEmailCode")
    public Result getEmailCode(@RequestParam String email) {
        return authService.sendMailCode(email);
    }

    @ApiOperation("注册")
    @PostMapping(value = "/register")
    public Result register(@RequestBody AuthUserDto authUserDto) {
        System.out.println(authUserDto);
        return authService.register(authUserDto);
    }

    @PostMapping("/userAuth")
    @ApiOperation("得到用户权限信息")
    public Result userRole(@RequestBody AuthUsername authUsername){
        if(authUsername==null)
            return new ErrorResult("未提供用户信息");
        List<String> userRoles = new ArrayList<>();

        AuthUserRolesDto authUserRolesDto = new AuthUserRolesDto();
        String userRoleInfo = userBaseInfoService.getUserRoleInfo(authUsername.getUsername());
        if(userRoleInfo==null)
            return new ErrorResult("找不到用户名");
        userRoles.add(userRoleInfo);


        authUserRolesDto.setRoles(userRoles);
        authUserRolesDto.setUsername(authUsername.getUsername());

        List<AuthUserRolesDto> authUserRolesDtos = new ArrayList<>();

        authUserRolesDtos.add(authUserRolesDto);
        return new SuccessResult(authUserRolesDtos);

    }
    @PostMapping("/userInfo")
    @ApiOperation("得到普通用户基本信息")
    public Result userInfo(@RequestBody AuthUserInfoDto authUserInfoDto){
        System.out.println(authUserInfoDto);
        return userBaseInfoService.getAllNormalUser(authUserInfoDto.getCurrentPage(),authUserInfoDto.getSize());
    }
}

